/* 
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */



$(function() {
    document.title = "Category Management";
    $('#dataTable').dataTable({
        "bPaginate": true,
        "bLengthChange": true,
        "bFilter": true,
        "bSort": true,
        "bInfo": true,
        "bAutoWidth": true
    });
    
    $("#form").validate({
		rules: {
                    name: "required",
		},
		messages: {
                    name: "Please enter name",
		},
                submitHandler: function(){
                    var data_form = $('#form').serialize();
                    console.log(data_form);
                    console.log($("#form"));
                    $.ajax({
                        type: 'post',
                        url: $('#form').attr('action'),
                        data: data_form,
                        dataType: 'json',
                        success: function(rs){
                            if (rs.status){
                                $('.notification').hide();
                                
                                $('.msgsuccess').text(rs.msg);
                                $('.notification').fadeIn('slow');
                                
                                 $('#dataTable').dataTable().fnAddData( [
                                    rs.insertedID,
                                    $('#name').val(),
                                    "Available",
                                    "<a href='editCategory.jsp?cid="+ rs.insertedID +"'>Edit</a> "
                                ] );
                                console.log(rs.msg);
                                /*$('html, body').animate({
                                    scrollTop: $('.msgsuccess').offset().top-30
                                }, 2000);*/
                                $('#form').trigger('reset');
                            }
                            else{
                                $('.notification').hide();
                                $('.msgerror').fadeIn('slow');
                                $('.msgerror').text(rs.msg);
                            }
                            var hideNoti = setTimeout(function(){
                                    $('.notification').fadeOut('slow');
                                },2000);
                            return false;
                        }
                    });
                    return false;
                }
                
	}); 
    
});


